package com.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author mayifan
 * @version 1.0
 * @date 2025-02-24 18:47
 */
@Configuration
public class MyRabbitConfig {
    /**
     * 使用JSON序列化机制，进行消息转换
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }


    //String name, boolean durable, boolean autoDelete, Map<String, Object> arguments
//    库存交换机
    @Bean
    public Exchange stockEventExchange(){
        return new TopicExchange("stock-event-exchange",true,false);
    }

    //    库存队列
    @Bean
    public Queue stockReleaseStockQueue(){
        return new Queue("stock.release.stock.queue",true,false,false);
    }

    //    死信队列
    @Bean
    public Queue stockDelayQueue(){
        Map<String,Object> argument = new HashMap<>();
        argument.put("x-dead-letter-exchange", "stock-event-exchange");
        argument.put("x-dead-letter-routing-key", "stock.release");
        argument.put("x-message-ttl", 20000);
        Queue queue = new Queue("stock.delay.queue", true, false, false,argument);
        return queue;
    }



    @Bean
    public Binding stockReleaseStockBinding() {
        return  new Binding("stock.release.stock.queue", Binding.DestinationType.QUEUE,
                "stock-event-exchange","stock.release.#",null);
    }


    @Bean
    public Binding stockLockedBinding() {
        return new Binding("stock.delay.queue", Binding.DestinationType.QUEUE,
                "stock-event-exchange","stock.locked",null);
    }

}